package edu.cs.usask.ca.silver.plugin.rcp.editors;

import java.util.Date;

import org.apache.log4j.Logger;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;

import edu.cs.usask.ca.silver.businesslogic.ids.ScenarioID;
import edu.cs.usask.ca.silver.plugin.rcp.dao.SilverBusinessLogicController;
import edu.cs.usask.ca.silver.plugin.rcp.jobs.LoggerReportScheduler;

/**
 * This class provides the structure for the table viewer displayed in the
 * scenario collection editor pane. The class lays out the column names, order
 * and data associated with scenarios with in the collection Again all data that
 * is displayed here is retrieved from the database.
 * 
 * @author Mike Taylor
 * 
 */
public class ScenarioTableColumnLabelProvider {

	public InfoEditor infoEditor;

	private static Logger logger = Logger
			.getLogger(ScenarioTableColumnLabelProvider.class);

	public ScenarioTableColumnLabelProvider(InfoEditor infoEditor) {
		this.infoEditor = infoEditor;

	}/* end of the constructor */

	/**
	 * A method that actually fleshes out the data to be displayed in the table
	 * viewer for the scenarios that belong to a scenario collections.
	 * 
	 * @param viewer
	 *            the table viewer that these column belong
	 */
	public void createColumns(final TableViewer viewer) {

		// Column 0: Name
		TableViewerColumn nameColumn = new TableViewerColumn(viewer, SWT.NONE);
		nameColumn.getColumn().setWidth(150);
		nameColumn.getColumn().setText("Scenario Name");
		nameColumn.getColumn().setMoveable(true);

		nameColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				try {
					return ((ScenarioID) element)
							.getEntryNameFromDatabase(infoEditor
									.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,
							"error while getting name for scenario column label provider",
							e);
				}
				return null;
			}
		});

		// Column 1: Type
		TableViewerColumn creatorColumn = new TableViewerColumn(viewer,
				SWT.NONE);
		creatorColumn.getColumn().setWidth(125);
		creatorColumn.getColumn().setText("Scenario Creator");
		creatorColumn.getColumn().setMoveable(true);
		creatorColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				try {
					return ((ScenarioID) element)
							.getCreatorNameFromDatabase(infoEditor
									.getAssociatedStorage());
				} catch (NullPointerException e1) {
					LoggerReportScheduler.scheduleErrorLogging(logger,
							"Null Pointer Exception getting creator name for scenario column label provider",
							e1);
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,
							"Exception getting creator name for scenario column label provider",
							e);
				}
				return null;

			}
		});

		// Column 2: Type
		TableViewerColumn descriptionColumn = new TableViewerColumn(viewer,
				SWT.NONE);
		descriptionColumn.getColumn().setWidth(275);
		descriptionColumn.getColumn().setText("Scenario Description");
		descriptionColumn.getColumn().setMoveable(true);

		descriptionColumn.setLabelProvider(new ColumnLabelProvider() {

			@Override
			public String getText(Object element) {
				String desc = null;
				try {
					desc = SilverBusinessLogicController.borrowScenario()
							.getDescription((ScenarioID) element,
									infoEditor.getAssociatedStorage());
				} catch (NullPointerException e1) {
					LoggerReportScheduler.scheduleErrorLogging(logger,
							"NullPointerException while getting description for scenario column label provider",
							e1);

				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,
							"Exception while getting description for scenario column label provider",
							e);
				}
				SilverBusinessLogicController.returnLastScenarioObject();
				return desc;

			}
		});

		// Column 3: Runtime Start Time
		TableViewerColumn RunStartTimeColumn = new TableViewerColumn(viewer,
				SWT.NONE);
		RunStartTimeColumn.getColumn().setWidth(150);
		RunStartTimeColumn.getColumn().setText("Run Start Time");
		RunStartTimeColumn.getColumn().setMoveable(true);

		RunStartTimeColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				Date time = null;

				try {
					time = SilverBusinessLogicController.borrowScenario()
							.getScenarioRunStartTime((ScenarioID) element,
									infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error getting run start time for simulation",
							e);
				}

				if (time == null) {
					Label noTime = new Label(viewer.getTable().getShell(),
							SWT.NONE);
					noTime.setText("No start time Available");
					noTime.setForeground(PlatformUI.createDisplay()
							.getSystemColor(SWT.COLOR_GRAY));
					return noTime.getText();
				}

				return time.toString();
			}
		});

		// Column 4: Runtime End Time
		TableViewerColumn RunEndTimeColumn = new TableViewerColumn(viewer,
				SWT.NONE);
		RunEndTimeColumn.getColumn().setWidth(150);
		RunEndTimeColumn.getColumn().setText("Run End Time");
		RunEndTimeColumn.getColumn().setMoveable(true);

		RunEndTimeColumn.setLabelProvider(new ColumnLabelProvider() {

			// @SuppressWarnings("unchecked")
			@Override
			public String getText(Object element) {
				Date time = null;

				try {
					time = SilverBusinessLogicController.borrowScenario()
							.getScenarioRunEndTime((ScenarioID) element,
									infoEditor.getAssociatedStorage());
				} catch (Exception e) {
					LoggerReportScheduler.scheduleErrorLogging(logger,"error getting run end time for simulation",
							e);
				}

				if (time == null) {
					Label noTime = new Label(viewer.getTable().getShell(),
							SWT.NONE);
					noTime.setText("No end time Available");
					noTime.setForeground(PlatformUI.createDisplay()
							.getSystemColor(SWT.COLOR_GRAY));
					return noTime.getText();
				}

				return time.toString();
			}
		});

	}/* end of column construction */

}/* end of the ScenarioTableColumnLabelProvider class */
